package com.we.risk.phoneBook.adm;

import org.apache.flink.api.java.utils.ParameterTool;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Set;

import static com.we.risk.phoneRecord.common.Stage2GlobalSetting.*;
import static com.we.risk.phoneRecord.stage2.dimphone.DimPhoneRecordLabelBroadCast.OUTPUTKEY;

public class dataCmp {
    public static final String PROPSETS = "risk/phonebook/phonebook230126.properties";
    public static Logger LOG = LoggerFactory.getLogger(dataCmp.class);

    public static final String DATEFORMAT = "yyyy-MM-dd";
    public static final String INPUTKEYBY = "user_key";
    public static final String TIMESTAMP = "timestamp";
    public static final String CAPTURETIME = "capture_time";
    public static final String OUTKEYBY = "user_key";
    public static final String LABELOUTIME = "label_output_time";
    public static final String OUTJOBID = "job_id";
    public static final String INPUTJOBID = "jobID";
    public static final String PHONEBOOKLIST = "phoneBookList";

    public static JSONObject ProcessData(JSONObject inputJson, JSONObject userState) {

        //        JSONObject inputJson = JSONObject.parseObject(value);
        Long captureTime = inputJson.getLong(TIMESTAMP);
        String user_key = inputJson.getString(INPUTKEYBY);
        String job_id = inputJson.getString(INPUTJOBID);
        JSONArray phoneBookList = inputJson.getJSONArray(PHONEBOOKLIST);
        int adb_cnt = phoneBookList.size();
        LOG.debug("Debug adb_cnt: " + adb_cnt);

        userBasicInfo basicInfo = new userBasicInfo();
        for (int i = 0; i < adb_cnt; i++) {
            phoneBookObject phoneBookObject =
                    new phoneBookObject((JSONObject) phoneBookList.get(i), userState);
            basicInfo.process(phoneBookObject);
        }

        JSONObject resJson = new JSONObject();
        resJson.put(OUTKEYBY, user_key);
        resJson.put(CAPTURETIME, captureTime);
        resJson.put(OUTJOBID, job_id);
        basicInfo.format(resJson);
        long label_output_time = System.currentTimeMillis();
        resJson.put(LABELOUTIME, label_output_time);
        System.out.println("Debug resJson size: " + resJson.size());
        System.out.println("Debug resJson: " + resJson.toString());
        return resJson;
    }

    private static JSONObject initPropJson(String propFile) throws IOException {
        InputStream resourceAsStream = dataCmp.class.getClassLoader().getResourceAsStream(propFile);
        ParameterTool reader = ParameterTool.fromPropertiesFile(resourceAsStream);
        JSONObject res = new JSONObject();
        Set<Object> keySet = reader.getProperties().keySet();
        //        System.out.println("initPropJson keySet size: " + keySet.size());
        Iterator<Object> keysIterator = keySet.iterator();
        while (keysIterator.hasNext()) {
            res.put((String) keysIterator.next(), -1);
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        try {
            //            String user_input =
            // "{\"pid_city\":\"哈尔滨市\",\"mob_city\":\"哈尔滨市\",\"mob_isp\":\"中国电信\",\"ad_prov\":\"黑龙江省\",\"mob_prov\":\"黑龙江省\",\"pid_prov\":\"黑龙江省\",\"user_key\":\"05d75795a1588a397dad1399013b4a9d\",\"ad_city\":\"哈尔滨市\"}";
            //            String user_input =
            // "{\"pid_city\":\"呼伦贝尔市\",\"mob_city\":\"呼伦贝尔市\",\"mob_isp\":\"中国移动\",\"ad_prov\":\"内蒙古自治区\",\"mob_prov\":\"内蒙古自治区\",\"pid_prov\":\"内蒙古自治区\",\"user_key\":\"b2c47c79721d0671588bb833689bf84b\",\"ad_city\":\"呼伦贝尔市\"}";
            //            String user_input =
            // "{\"pid_city\":\"开封市\",\"mob_city\":\"开封市\",\"mob_isp\":\"中国移动\",\"ad_prov\":\"河南省\",\"mob_prov\":\"河南省\",\"pid_prov\":\"河南省\",\"user_key\":\"791049d864b6500bf6b72e1c82c9db0e\",\"ad_city\":\"开封市\"}\n";
            //            String user_input =
            // "{\"pid_city\":\"保定市\",\"mob_city\":\"保定市\",\"mob_isp\":\"中国移动\",\"ad_prov\":\"河北省\",\"mob_prov\":\"河北省\",\"pid_prov\":\"河北省\",\"user_key\":\"5b32a74fa87fe4929ccd89289d4cc9fd\",\"ad_city\":\"保定市\"}";
            //            String user_input =
            // "{\"pid_city\":\"孝感市\",\"mob_city\":\"武汉市\",\"mob_isp\":\"中国移动\",\"ad_prov\":\"湖北省\",\"mob_prov\":\"湖北省\",\"pid_prov\":\"湖北省\",\"user_key\":\"e6c1199e8fa8bbdedb844333a1eb75de\",\"ad_city\":\"武汉市\"}";
            // f3e639845633f8d7a65c0f329ddaecc6
            //            String user_input =
            // "{\"pid_city\":\"庆阳市\",\"mob_city\":\"咸宁市\",\"mob_isp\":\"中国移动\",\"ad_prov\":\"湖北省\",\"mob_prov\":\"湖北省\",\"pid_prov\":\"甘肃省\",\"user_key\":\"567aabd9d23e124de00816c71778dc05\",\"ad_city\":\"武汉市\"}";
            // ba133bfcc0494f05da6d2444658cf94c
            // String user_input =
            // "{\"pid_city\":\"天津市\",\"mob_city\":\"天津市\",\"mob_isp\":\"中国电信\",\"ad_prov\":\"天津市\",\"mob_prov\":\"天津市\",\"pid_prov\":\"天津市\",\"user_key\":\"ba133bfcc0494f05da6d2444658cf94c\",\"ad_city\":\"天津市\"}";
            // aa0f1e864fdfb80bf8f53c27fb16b728
            // String user_input =
            // "{\"pid_city\":\"北海市\",\"mob_city\":\"上海市\",\"mob_isp\":\"中国移动\",\"ad_prov\":\"上海市\",\"mob_prov\":\"上海市\",\"pid_prov\":\"广西壮族自治区\",\"user_key\":\"aa0f1e864fdfb80bf8f53c27fb16b728\",\"ad_city\":\"上海市\"}";
            // dfb886c1497e162b69d3aa6fa13c3626
            //            String user_input =
            // "{\"pid_city\":\"黔西南布依族苗族自治州\",\"mob_city\":\"黔西南布依族苗族自治州\",\"mob_isp\":\"中国移动\",\"ad_prov\":\"贵州省\",\"mob_prov\":\"贵州省\",\"pid_prov\":\"贵州省\",\"user_key\":\"dfb886c1497e162b69d3aa6fa13c3626\",\"ad_city\":\"遵义市\"}";
            // 781e526d5b916bd188f18541ce2a6d79
            //            String user_input =
            // "{\"pid_city\":\"盘锦市\",\"mob_city\":\"盘锦市\",\"mob_isp\":\"中国移动\",\"ad_prov\":\"辽宁省\",\"mob_prov\":\"辽宁省\",\"pid_prov\":\"辽宁省\",\"user_key\":\"781e526d5b916bd188f18541ce2a6d79\",\"ad_city\":\"盘锦市\"}";
            // d54b8cf5f7b13d9db1ae33362a13bd5a
            String user_input =
                    "{\"pid_city\":\"泉州市\",\"mob_city\":\"泉州市\",\"mob_isp\":\"中国移动\",\"ad_prov\":\"福建省\",\"mob_prov\":\"福建省\",\"pid_prov\":\"福建省\",\"user_key\":\"d54b8cf5f7b13d9db1ae33362a13bd5a\",\"ad_city\":\"泉州市\"}";
            File file =
                    new File(
                            "/Users/jk/Downloads/风控标签需求/实时需求/2023/0112/通讯录/cmp/phone_book_compare.data");
            String pr_input = FileUtils.readFileToString(file);

            JSONObject inputJson = JSONObject.parseObject(pr_input);
            String user_key = inputJson.getString(OUTPUTKEY);

            System.out.println("enter!!");
            JSONObject tmpJson = ProcessData(inputJson, JSONObject.parseObject(user_input));

            JSONObject curState = initPropJson(PROPSETS);
            curState.put(OUTPUTKEY, user_key);
            Iterator<String> iterator = tmpJson.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                if (!key.equals(OUTKEYBY) && curState.containsKey(key)) {
                    Object labelV = inputJson.get(key);
                    if (!curState.get(key).equals(labelV)) {
                        //                            JSONObject resJson = new JSONObject();
                        //                            resJson.put(OUTKEYBY, userKey);
                        //                            resJson.put("label_name", key);
                        //                            resJson.put("label_value", labelV);
                        curState.put(key, labelV);
                    }
                }
            }

            //            System.out.println("curState:" + curState.toString());

        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }
}
